import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

import vitePluginVueSetupExtend from 'vite-plugin-vue-setup-extend'

import { viteMockServe } from 'vite-plugin-mock'

import path from 'path'

// https://vitejs.dev/config/

const pathSrc = path.resolve(__dirname, 'src')

//写法一
// export default defineConfig({
//   plugins: [
//     vue(),
//     viteMockServe({
//       mockPath: 'mock',
//       enable: true,
//     }
//     ),
//     vitePluginVueSetupExtend()
//   ],
//   base: './',
//   resolve: {
//     alias: {
//       '@/': `${pathSrc}/`,
//     },
//   },
//   css: {
//     preprocessorOptions: {
//       scss: {
//         javascriptEnabled: true,
//         additionalData: `@import "@/style/variable.scss";`,
//       },
//     },
//   },
// })

//写法二
export default defineConfig(({ command, mode }) => {
  //获取各种环境下的变量
  let env = loadEnv(mode, process.cwd())

  return {
    plugins: [
      vue(),
      viteMockServe({
        mockPath: 'mock',
        enable: true,
      }
      ),
      vitePluginVueSetupExtend()
    ],
    base: './',
    resolve: {
      alias: {
        '@/': `${pathSrc}/`,
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: `@import "@/style/variable.scss";`,
        },
      },
    },
    //代理跨域
    server: {
      proxy: {
        [env.VITE_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
})